//
//  InspirationVC.m
//  NewNest
//
//  Created by hesiyao on 16/1/23.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "InspirationVC.h"
#import "InspirationModal.h"
#import "InspirationCell.h"
#import <AFNetworking.h>
#import <AFHTTPSessionManager.h>
#import <UIImageView+WebCache.h>
#import "InspirationDetailPageVC.h"
@interface InspirationVC () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableview;
@property (nonatomic, retain) NSMutableArray *dataArr;
@end

@implementation InspirationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //navigation
    self.navigationItem.title = @"生活灵感";
    
    // 处理数据
    [self dataHandler];
    
    // 创建视图
    [self createSubviews];
}

/// 处理数据
- (void)dataHandler
{
    NSString *urlStr = @"http://ihome.cmfmobile.com:8080/sp/custom/getNewSpecial.do?pn=1";
    [[AFHTTPSessionManager manager] GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"灵感专题解析成功");
        self.dataArr = [InspirationModal modelHanderWithArray:responseObject];
        
        [_tableview reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"灵感专题解析失败");
    }];
}

/// 创建视图
- (void)createSubviews
{
    self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [_tableview registerClass:[InspirationCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableview];
    [_tableview release];
}

// 设置cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

//设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InspirationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    InspirationModal *m = _dataArr[indexPath.row];
    cell.titleLabel.text = m.title;
    cell.contentLabel.text = m.introduction;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.pic]];
    return cell;
}

// 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height / 6;
}

// 点击进入详情
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    InspirationDetailPageVC *detailVC = [[InspirationDetailPageVC alloc] init];
    InspirationModal *m = _dataArr[indexPath.row];
    detailVC.idStr = m.detailId;
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
